package graphic;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import core.Client;

public class MainFrame extends JFrame {

	protected static MainFrame MAINFRAME;
	protected static WindowAdapter WINDOWADAPTER;
	
	JPanel centerPanel;

	ToolBar toolBar;
	UsersPanel usersPanel;
	FilesTabbedPanel filesPanel;
	ReleasesPanel releasesPanel;
	
	public MainFrame() {
		super("Client");
		
		this.centerPanel = new JPanel(new BorderLayout());

		this.centerPanel.add(ConnectionPanel.getInstance(), BorderLayout.CENTER);
		
		int x = (int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() /2) - 100;
		int y = (int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight() /2) - 100;

		super.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		super.getContentPane().add(this.centerPanel);
		super.validate();
		super.pack();
		super.setVisible(true);
		super.setLocation(x, y);
	}
	
	public void paintClientPanel()
	{
		this.centerPanel.removeAll();
	
		super.setTitle("Client: " + Client.getInstance().getUserId());
		
		JPanel releasePanel = new JPanel(new BorderLayout());
		releasePanel.add(ReleasesPanel.getInstance(), BorderLayout.CENTER);
		releasePanel.add(ReleaseNotePanel.getIstance(), BorderLayout.SOUTH);
		
		this.toolBar = ToolBar.getIstance();
		this.usersPanel = UsersPanel.getInstance();
		this.filesPanel = FilesTabbedPanel.getIstance();
		this.releasesPanel = ReleasesPanel.getInstance();
		
		this.centerPanel.add(this.toolBar, BorderLayout.NORTH);
		this.centerPanel.add(this.usersPanel, BorderLayout.WEST);
		this.centerPanel.add(this.filesPanel, BorderLayout.CENTER);
		this.centerPanel.add(releasePanel, BorderLayout.EAST);
		
		super.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		
		super.removeWindowListener(MainFrame.getWindowAdapter());
		super.addWindowListener(MainFrame.getWindowAdapter());
		
		super.getContentPane().removeAll();
		super.getContentPane().add(this.centerPanel);
		
		
		HashMap<String, ProgramTab> tabs = FilesTabbedPanel.getIstance().tabs; 
		
		for (Iterator iterator = tabs.keySet().iterator(); iterator.hasNext();)
		{
			ProgramTab  tab = tabs.get((String)iterator.next());
			tab.refreshTable();			
		}

		int x = (int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() /2) - (int)super.getSize().getWidth() - 100;
		int y = (int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight() /2) - (int)super.getSize().getWidth() - 100;
		
		super.validate();
		super.pack();
		super.setLocation(x, y);
		super.repaint();
	}
	
	public void close()
	{
		Client.getInstance().manuallyClose = true;
		Client.getInstance().stopClient();
		
		super.dispose();
	}

	public void showMessage(String message, int type)
	{
	    JOptionPane optionPane = new JOptionPane(message, type);
	    JDialog dialog = optionPane.createDialog(this, message);
	    dialog.pack();
	    
		Dimension dim = super.getSize();
		int x = (dim.width - dialog.getSize().width) / 2;
		int y = (dim.height - dialog.getSize().height) / 2;
	    
	    dialog.setLocation(x, y);
	    dialog.setVisible(true);
	}

	protected static WindowAdapter getWindowAdapter()
	{
		if(WINDOWADAPTER == null) 
		{
			WINDOWADAPTER = new WindowAdapter()
			{
				@Override
				public void windowClosing(WindowEvent e){
					MainFrame.getInstance().close();				
				}
			};
		}	
		
		return WINDOWADAPTER;
	}
	
	public static MainFrame getInstance()
	{
		if(MAINFRAME == null) MAINFRAME = new MainFrame();
		return MAINFRAME;
	}
}
